home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / ARP.C next >
C/C++ Source or Header  |  1988-11-29  |  10KB  |  355 lines

  1. /* Address Resolution Protocol (ARP) functions. Sits between IP and
  2.  * Level 2, mapping IP to Level 2 addresses for all outgoing datagrams.
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "timer.h"
  7. #include "iface.h"
  8. #ifndef ATARI_ST        /* DG2KK */
  9. #include "enet.h"
  10. #endif
  11. #include "ax25.h"
  12. #include "arp.h"
  13.  
  14. extern int32 ip_addr;        /* Our IP address */
  15.  
  16. /* ARP entries for particular subnetwork types. The table values
  17.  * are filled in by calls to arp_init() at device attach time
  18.  */
  19. #define    NTYPES    9
  20. struct arp_type arp_type[NTYPES];
  21.  
  22. /* Hash table headers */
  23. struct arp_tab *arp_tab[ARPSIZE];
  24.  
  25. struct arp_stat arp_stat;
  26.  
  27. /* Initialize an entry in the ARP table
  28.  * Called by the device driver at attach time
  29.  */
  30. arp_init(hwtype,hwalen,iptype,arptype,bdcst,format,scan)
  31. unsigned int hwtype;    /* ARP Hardware type */
  32. int hwalen;        /* Hardware address length */
  33. int iptype;        /* Subnet's protocol ID for IP */
  34. int arptype;        /* Subnet's protocol ID for ARP */
  35. char *bdcst;        /* Subnet's broadcast address (if any) */
  36. int (*format)();    /* Function to format hardware addresses */
  37. int (*scan)();        /* Function to scan addresses in ascii */    
  38. {
  39.     register struct arp_type *at;
  40.  
  41.     if(hwtype >= NTYPES)
  42.         return -1;    /* Table too small */
  43.  
  44.     at = &arp_type[hwtype];
  45.     at->hwalen = (int16)hwalen;
  46.     at->iptype = (int16)iptype;
  47.     at->arptype = (int16)arptype;
  48.     at->bdcst = bdcst;
  49.     at->format = format;
  50.     at->scan = scan;
  51.     return 0;
  52. }
  53.  
  54. /* Resolve an IP address to a hardware address; if not found,
  55.  * initiate query and return NULLCHAR.  If an address is returned, the
  56.  * interface driver may send the packet; if NULLCHAR is returned,
  57.  * res_arp() will have saved the packet on its pending queue,
  58.  * so no further action (like freeing the packet) is necessary.
  59.  */
  60. char *
  61. res_arp(interface,hardware,target,bp)
  62. struct interface *interface;    /* Pointer to interface block */
  63. int16 hardware;        /* Hardware type */
  64. int32 target;        /* Target IP address */
  65. struct mbuf *bp;    /* IP datagram to be queued if unresolved */
  66. {
  67.     void arp_output();
  68.     register struct arp_tab *arp;
  69.  
  70.     if((arp = arp_lookup(hardware,target)) != NULLARP && arp->state == ARP_VALID)
  71.         return arp->hw_addr;
  72.     /* Create an entry and put the datagram on the
  73.      * queue pending an answer
  74.      */
  75.     arp = arp_add(target,hardware,NULLCHAR,0);
  76.     enqueue(&arp->pending,bp);
  77.     arp_output(interface,hardware,target);
  78.     return NULLCHAR;
  79. }
  80. /* Handle incoming ARP packets. This is almost a direct implementation of
  81.  * the algorithm on page 5 of RFC 826, except for:
  82.  * 1. Outgoing datagrams to unresolved addresses are kept on a queue
  83.  *    pending a reply to our ARP request.
  84.  * 2. The names of the fields in the ARP packet were made more mnemonic.
  85.  */
  86. void
  87. arp_input(interface,bp)
  88. struct interface *interface;
  89. struct mbuf *bp;
  90. {
  91.     struct arp arp;
  92.     struct arp_tab *ap;
  93.     struct arp_type *at;
  94.     struct mbuf *htonarp();
  95.     
  96.     arp_stat.recv++;
  97.     if(ntoharp(&arp,&bp) == -1)    /* Convert into host format */
  98.         return;
  99.     if(arp.hardware >= NTYPES){
  100.         /* Unknown hardware type, ignore */
  101.         arp_stat.badtype++;
  102.         return;
  103.     }
  104.     at = &arp_type[arp.hardware];
  105.     if(arp.protocol != at->iptype){
  106.         /* Unsupported protocol type, ignore */
  107.         arp_stat.badtype++;
  108.         return;
  109.     }
  110.     if(arp.hwalen > MAXHWALEN || arp.pralen != sizeof(int32)){
  111.         /* Incorrect protocol addr length (different hw addr lengths
  112.          * are OK since AX.25 addresses can be of variable length)
  113.          */
  114.         arp_stat.badlen++;
  115.         return;
  116.     }
  117.     if(memcmp(arp.shwaddr,at->bdcst,at->hwalen) == 0){
  118.         /* This guy is trying to say he's got the broadcast address! */
  119.         arp_stat.badaddr++;
  120.         return;
  121.     }
  122.     /* If this guy is already in the table, update its entry
  123.      * unless it's a manual entry (noted by the lack of a timer)
  124.      */
  125.     ap = NULLARP;    /* ap plays the role of merge_flag in the spec */
  126.     if((ap = arp_lookup(arp.hardware,arp.sprotaddr)) != NULLARP
  127.      && ap->timer.start != 0){
  128.         ap = arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,arp.hwalen & 0xff);
  129.     }
  130.     /* See if we're the address they're looking for */
  131.     if(arp.tprotaddr == ip_addr){
  132.         if(ap == NULLARP)    /* Only if not already in the table */
  133.             arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,arp.hwalen & 0xff);
  134.  
  135.         if(arp.opcode == ARP_REQUEST){
  136.             /* Swap sender's and target's (us) hardware and protocol
  137.              * fields, and send the packet back as a reply
  138.              */
  139.             memcpy(arp.thwaddr,arp.shwaddr,arp.hwalen);
  140.             /* Mark the end of the sender's AX.25 address
  141.              * in case he didn't
  142.              */
  143.             if(arp.hardware == ARP_AX25)
  144.                 arp.thwaddr[arp.hwalen-1] |= E;
  145.  
  146.             memcpy(arp.shwaddr,interface->hwaddr,at->hwalen);
  147.             arp.tprotaddr = arp.sprotaddr;
  148.             arp.sprotaddr = ip_addr;
  149.             arp.opcode = ARP_REPLY;
  150.             bp = htonarp(&arp);
  151.             (*interface->output)(interface,arp.thwaddr,
  152.                 interface->hwaddr,at->arptype,bp);
  153.             arp_stat.inreq++;
  154.         } else {
  155.             arp_stat.replies++;
  156.         }
  157.     }
  158. }
  159. /* Add an IP-addr / hardware-addr pair to the ARP table */
  160. struct arp_tab *
  161. arp_add(ipaddr,hardware,hw_addr,hw_alen)
  162. int32 ipaddr;    /* IP address, host order */
  163. int16 hardware;    /* Hardware type */
  164. char *hw_addr;    /* Hardware address, if known; NULLCHAR otherwise */
  165. int16 hw_alen;    /* Length of hardware address */
  166. {
  167.     void arp_drop(),ip_route();
  168.     struct mbuf *bp,*dequeue();
  169.     register struct arp_tab *ap;
  170.     unsigned hashval,arp_hash();
  171.  
  172.     if((ap = arp_lookup(hardware,ipaddr)) == NULLARP){
  173.         /* New entry */
  174.         if((ap = (struct arp_tab *)calloc(1,sizeof(struct arp_tab))) == NULLARP)
  175.             return NULLARP;
  176.         ap->timer.func = arp_drop;
  177.         ap->timer.arg = (char *)ap;
  178.         ap->hardware = hardware;
  179.         ap->ip_addr = ipaddr;
  180.  
  181.         /* Put on head of hash chain */
  182.         hashval = arp_hash(hardware,ipaddr);
  183.         ap->prev = NULLARP;
  184.         ap->next = arp_tab[hashval];
  185.         arp_tab[hashval] = ap;
  186.         if(ap->next != NULLARP){
  187.             ap->next->prev = ap;
  188.         }
  189.     }
  190.     if(hw_addr == NULLCHAR){
  191.         /* Await response */
  192.         ap->state = ARP_PENDING;
  193.         ap->timer.start = PENDTIME;
  194.     } else {
  195.         /* Response has come in, update entry and run through queue */
  196.         ap->state = ARP_VALID;
  197.         ap->timer.start = ARPLIFE;
  198.         if(ap->hw_addr != NULLCHAR)
  199.             free(ap->hw_addr);
  200.         if((ap->hw_addr = malloc(hw_alen)) == NULLCHAR){
  201.             free((char *)ap);
  202.             return NULLARP;
  203.         }
  204.         memcpy(ap->hw_addr,hw_addr,hw_alen);
  205.         /* This kludge marks the end of an AX.25 address to allow
  206.          * for optional digipeaters (insert Joan Rivers salute here)
  207.          */
  208.         if(hardware == ARP_AX25)
  209.             ap->hw_addr[hw_alen-1] |= E;
  210.         while((bp = dequeue(&ap->pending)) != NULLBUF)
  211.             ip_route(bp,0);
  212.     }
  213.     start_timer(&ap->timer);
  214.     return ap;
  215. }
  216.  
  217. /* Remove an entry from the ARP table */
  218. void
  219. arp_drop(ap)
  220. register struct arp_tab *ap;
  221. {
  222.     unsigned arp_hash();
  223.  
  224.     if(ap == NULLARP)
  225.         return;
  226.     stop_timer(&ap->timer);    /* Shouldn't be necessary */
  227.     if(ap->next != NULLARP)
  228.         ap->next->prev = ap->prev;
  229.     if(ap->prev != NULLARP)
  230.         ap->prev->next = ap->next;
  231.     else
  232.         arp_tab[arp_hash(ap->hardware,ap->ip_addr)] = ap->next;
  233.     if(ap->hw_addr != NULLCHAR)
  234.         free(ap->hw_addr);
  235.     free_q(&ap->pending);
  236.     free((char *)ap);
  237. }
  238.  
  239. /* Look up the given IP address in the ARP table */
  240. struct arp_tab *
  241. arp_lookup(hardware,ipaddr)
  242. int16 hardware;
  243. int32 ipaddr;
  244. {
  245.     unsigned arp_hash();
  246.     register struct arp_tab *ap;
  247.  
  248.     for(ap = arp_tab[arp_hash(hardware,ipaddr)]; ap != NULLARP; ap = ap->next){
  249.         if(ap->ip_addr == ipaddr && ap->hardware == hardware)
  250.             break;
  251.     }
  252.     return ap;
  253. }
  254. /* Send an ARP request to resolve IP address target_ip */
  255. static
  256. void
  257. arp_output(interface,hardware,target)
  258. struct interface *interface;
  259. int16 hardware;
  260. int32 target;
  261. {
  262.     struct arp arp;
  263.     struct mbuf *bp,*htonarp();
  264.     struct arp_type *at;
  265.  
  266.     at = &arp_type[hardware];
  267.     if(interface->output == NULLFP)
  268.         return;
  269.     
  270.     arp.hardware = hardware;
  271.     arp.protocol = at->iptype;
  272.     arp.hwalen = at->hwalen;
  273.     arp.pralen = sizeof(int32);
  274.     arp.opcode = ARP_REQUEST;
  275.     memcpy(arp.shwaddr,interface->hwaddr,at->hwalen);
  276.     arp.sprotaddr = ip_addr;
  277.     memset(arp.thwaddr,0,at->hwalen);
  278.     arp.tprotaddr = target;
  279.     bp = htonarp(&arp);
  280.     (*interface->output)(interface,at->bdcst,
  281.         interface->hwaddr,at->arptype,bp);
  282.     arp_stat.out